package com.chixing.day02_switch_case.test;

import com.chixing.day02_switch_case.SortUtil;

import java.util.ArrayList;
import java.util.Arrays;

public class Work2 {
    /**
     * *2. 给定一个包含 n 个整数的数组 nums，判断 nums 中是否存在三个元素 a ，b ，c ，使得 a + b + c = 0 ？请找出所有和为 0 且 不重复 的三元组。**
     *
     * 思路：
     * @param args
     */
    public static void main(String[] args) {
        int[] nums={-1,0,1,2,-1,4};
//        int[] nums={};
        int[][] arr=new int[100][3];
        int size=0;
        for (int i = 0; i < nums.length-2; i++) {
            for (int j = i+1; j < nums.length-1; j++) {
                for (int k = j+1; k < nums.length; k++) {
                    if((nums[i]+nums[j]+nums[k])==0) {
                        arr[size][0]=nums[i];
                        arr[size][1]=nums[j];
                        arr[size++][2]=nums[k];
                    }
                }
            }
        }
        //考虑不重复情况
            //1.二维数组按行排序
        for (int i = 0; i < size; i++) {
            SortUtil.bubbleSort(arr[i]);
        }

//        2.寻找数组完全相同的下标
        for (int i = 0; i < size-1; i++) {
            for (int j = i+1; j < size; j++) {
                if((arr[i][0]==arr[j][0])&&(arr[i][1]==arr[j][1])&&(arr[i][2]==arr[j][2])) {
                    //满足条件对二维数组进行降重后面数组依次前移
                    for (int k = j; k < size-1; k++) {
                        arr[k]=arr[k+1];
                    }
                    size--;
                }
            }
        }

        System.out.print("[");
        //遍历
        for (int i = 0; i < size; i++) {
            if (i==size-1)
                System.out.print("["+arr[i][0]+","+arr[i][1]+","+arr[i][2]+"]");
            else
                System.out.print("["+arr[i][0]+","+arr[i][1]+","+arr[i][2]+"],");
        }
        System.out.print("]");
    }
}
